package cn.lzd.mr.case1;


import org.apache.commons.collections.map.HashedMap;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.Map;

/**
 * Created by lzd on 2018/5/31.
 * key是要分区的字段，value分区要键入的值，KV也是MAP输出的key,value.
 * return 返回的结果值就是分区号
 */
public class MyPartition extends Partitioner<Text, Bean> {

    int partition = 4;

    static Map<String, Integer> map = new HashedMap() {{
        put("135", 0);
        put("136", 1);
        put("137", 2);
        put("138", 3);
    }};

    @Override
    public int getPartition(Text text, Bean bean, int i) {
        Integer code = map.get(text.toString().substring(0, 3));
        return code == null ? partition : code;
    }
}
